/**
 * @author 黄泽毅
 */

function transfer() {
    let myId = getQueryVariable("id");
    let myData = getDataFromLocal("questionnaires");
    localStorage.setItem("dataId",myId);
    if (myData.length < myId || myId <=0) {
        location.href = "new.html";
    }
    decPageView(myData[myId-1],1);
}

transfer();

//遍历获取数据，显示数据
function showData(result) {
    //获取选项的模板(单选题)
    let qu_template = $("#q_que_li").innerHTML;
    //获取html模板(单选)
    let qr_template = $("#question_radio").innerHTML;
    //获取html模板(多选)
    let qc_template = $("#question_checkbox").innerHTML;
    //获取html模板(文本)
    let qt_template = $("#question_text").innerHTML;
    //获取HTML模板(多选题)
    let ql2_template = $("#q_que_li2").innerHTML;
    //获取文本题百分比数据条的模板
    let ql3_template = $("#word_per").innerHTML;

    //判断选择类型
    let list = result.list;


    //渲染一个标题
    let big_title = result.title;
    $("#headline").innerHTML = big_title;

    //一个问题的选项列表
    let qu_li_lists = "";
    let templi = "";
    //一个问题
    let q_lists = "";

    //计算一个问题中总回答的人数
    let sum1 = 0;

    //得到百分比
    let per;

    //创建一个数组来接收用于图表的人数和名称(存对象)
    let table_data1 = [];
    let obj = {};
    let ei;


    //清除所有q标签清缓存
    Array.from($(".q")).forEach(item => {
        item.remove();
    });

    list.forEach(item => {

        //先得到items
        let items = item.items;
        sum1 = 0;
        table_data1 = [];
        qu_li_lists = "";
        //判断类型
        if (item.type === "radio") {
            //再来循环一层得到总票数
            items.forEach(ems => {
                sum1 += ems.selected;
            })

            if (sum1 == 0) {
                sum1 = 1;
            }

            //再循环一层得到问题的选项列表
            items.forEach(ems => {
                per = Math.round(ems.selected / sum1 * 10000) / 100.00 + "%";

                templi = qu_template.replace("{title}", ems.content)
                    .replace("{percent}", per)
                    .replace("{mywidth}", per);
                qu_li_lists += templi;
            })

            q_lists = qr_template.replace("{num}", "Q" + item.id)
                .replace("{headline}", item.title)
                .replace("{lihtml}", qu_li_lists);

            q_lists = q_lists.replaceAll("\n", "");
            q_lists = q_lists.trim();
            let p = parseDom(q_lists);
            $(".analysis")[0].appendChild(p[0]);

        } else if (item.type === "checkbox") {
            ei = "#table_c" + item.id;
            //再循环一层得到问题的选项列表
            items.forEach(ems => {
                obj = {};
                obj.value = ems.selected;
                obj.name = ems.content;
                table_data1.push(obj);
                templi = ql2_template.replace("{opts}", ems.content);
                qu_li_lists += templi;
            })

            //创建一个div元素的字符串
            let stringDiv = "<div id=table_c" + item.id + "> </div>";

            q_lists = qc_template.replace("{num}", "Q" + item.id)
                .replace("{headline}", item.title)
                .replace("{table_li}", qu_li_lists)
                .replace("{t_ch}", stringDiv);

            q_lists = q_lists.replaceAll("\n", "");
            q_lists = q_lists.trim();
            let p = parseDom(q_lists);
            $(".analysis")[0].appendChild(p[0]);
            show_chart(table_data1, ei);

        } else if (item.type === "text") {
            //有效文本
            //得到总的回答文本的个数(不回答的为0)
            let sum2 = item.sumOfWordsList.length;
            if (sum2 == 0) {
                sum2 = 1;
            }

            //得到数组
            let sowList = item.sumOfWordsList;
            //统计有效文本
            let effective = 0;

            for (let e in sowList) {
                if (sowList[e] != 0) {
                    effective++;
                }
            }


            //再循环一层得到问题的选项列表
            per = Math.round(effective / sum2 * 10000) / 100.00 + "%";
            templi = ql3_template.replace("{percent}", per)
                .replace("{mywidth}", per);

            qu_li_lists += templi;

            q_lists = qt_template.replace("{num}", "Q" + item.id)
                .replace("{headline}", item.title)
                .replace("{lihtml}", qu_li_lists);

            q_lists = q_lists.replaceAll("\n", "");
            q_lists = q_lists.trim();
            let p = parseDom(q_lists);
            $(".analysis")[0].appendChild(p[0]);
        }

    });
}
